package com.xiaolin.practice.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xiaolin.practice.pojo.WorkOrder;
import com.xiaolin.practice.pojo.WorkOrderDays;
import com.xiaolin.practice.pojo.WorkOrderMetrics;
import com.xiaolin.practice.pojo.WorkOrderTypes;
import com.xiaolin.practice.service.WorkOrderService;
import com.xiaolin.practice.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private WorkOrderService workOrderService;
    @PostMapping("/save")
    public R saveWorkOrder(@RequestBody WorkOrder workOrder) {
        System.out.println("controller saveWorkOrder");
        if(workOrderService.saveWorkOrder(workOrder)){
            return R.error("已有该工单，请重新输入");
        }
        return R.ok("添加成功");
    }

    @PostMapping("/delete/{id}")
    public R deleteWorkOrder(@PathVariable("id") Long id) {
        workOrderService.removeById(id);
        return R.ok("删除成功");
    }

    /**
     * 修改回写
     * @param id
     * @return
     */
    @PostMapping("/update_find/{id}")
    public R updateFindWorkOrder(@PathVariable("id") Long id) {
        Map<String,Object>  workOrderMap = new HashMap<>();
        workOrderMap.put("work",workOrderService.getById(id));
        return R.ok(workOrderMap);
    }
    @PostMapping("/update")
    public R updateWorkOrder(@RequestBody WorkOrder workOrder) {
        if (workOrderService.updateByWorkOrder(workOrder)){
            return R.error("已有该工单，修改失败");
        }
        return R.ok("修改成功");
    }

    @PostMapping("/page")
    public R selectPage(@RequestParam(value = "page", defaultValue = "1") int page,
                        @RequestParam(value = "size", defaultValue = "10") int size,
                        @RequestParam Map<String, Object> workOrderMap) {

        Map<String,Object> pageWork = new HashMap<>();
        Page<WorkOrder> pageParam = new Page<>(page, size);
        IPage<WorkOrder>  workOrderIPage = workOrderService.pageWorkOrder(pageParam,workOrderMap);

        pageWork.put("page",workOrderIPage);
        return R.ok(pageWork);
    }

    @PostMapping("/fenpai")
    public R fenpai(@RequestParam("orderId") Long orderId,
                    @RequestParam("departmentId") Long departmentId,
                    @RequestParam("departmentName") String departmentName){

        if (workOrderService.fenpai(orderId,departmentId,departmentName)){
           return R.ok("操作成功");
        }

        return R.error("部门id无效");
    }


//    http://localhost:8080/order/tj?dateMM=2023-07
    /**
     * 统计查询7月每天的工单总量、超期率
     * @param dateMM
     * @return
     */
    @PostMapping("/tj")
    public R  tj(@RequestParam("dateMM") String dateMM){
        List<WorkOrderDays> workOrderMetricsList = workOrderService.samplingOverdue(dateMM);
        Map<String,Object> workOrderMap = new HashMap<>();
        workOrderMap.put("workOrderMetricsList",workOrderMetricsList);
        return R.ok(workOrderMap);
    }
    @PostMapping("/orderType")
    public R tjWorkOrderTypes(@RequestParam("dateMM") String dateMM){
        Map<String,Object> workOrderMap = new HashMap<>();
        List<WorkOrderTypes> workOrderTypesList = workOrderService.getWorkOrderTypes(dateMM);
        workOrderMap.put("list",workOrderTypesList);
        return R.ok(workOrderMap);
    }
}
